/*****************************************************************
IIITB MTech 1st semester Data Management project.
A simple Virtual File System.

This currently holds implementation of a free list.
- It is not persistent and is wiped as soon as the program
is closed.
*****************************************************************/
#ifndef STANDARD_IO
#define STANDARD_IO

#include<stdio.h>

#endif

#ifndef STANDARD_LIB
#define STANDARD_LIB

#include<stdlib.h>

#endif

#include"../include/global_header.h"

void make_free_list(int size) {
	long int no_of_blocks = (size * 1024)/BLOCK_SIZE;
	int i=0;

	free_list = (char *) malloc(no_of_blocks * sizeof(char));

	for(i=0;i<no_of_blocks;i++) {
		free_list[i] = 1;                           // 1 - free block
	}                                               // 0 - used block
}

void make_fd_array() {
    long int no_of_blocks = (total_size * 1024)/BLOCK_SIZE;
	long int i=0;

	fd_array = (struct fd *) malloc(no_of_blocks * sizeof(struct fd));
	for(i=0;i<no_of_blocks;i++) {
		fd_array[i].parent = NULL;
		fd_array[i].child = NULL;
		fd_array[i].sibling = NULL;
		fd_array[i].type = 'e';
        fd_array[i].block_num = -1; 
	}
}

long int get_free_block() {
	long int i = 0;
    long int no_of_blocks = (total_size * 1024)/BLOCK_SIZE;

	while(i<no_of_blocks && free_list[i] != 1) {
		i++;
	}

    if(i<no_of_blocks) {
        free_list[i] = 0;
        return i;
    }
    else
        return -1;
}
